﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Library.Test.Logic
{

    /// <summary>
    /// 泛型委托
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="S"></typeparam>
    /// <param name="s1"></param>
    /// <param name="s2"></param>
    /// <param name="s3"></param>
    /// <returns></returns>
    public delegate string ProcessDelegate<T, S>(T s1, S s2, List<S> s3);
    /// <summary>
    /// 通知委托
    /// </summary>
    /// <param name="value"></param>
    public delegate void Notify(int value);

    /// <summary>
    /// 委托测试
    /// </summary>
    public class DelegateTestService
    {
        /// <summary>
        /// 声明了一个Delegate对象[其实就是一个事件]
        /// </summary>
        public ProcessDelegate<string, int> delegateMethod;

        /// <summary>
        /// 输出字符串
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="ii"></param>
        /// <param name="iii"></param>
        /// <returns></returns>
        public string OutputStr(string ss, int ii, List<int> iii)
        {
            string result = ss + ii;
            if (iii!=null&& iii.Any())
            {
                result += string.Join(",", iii);
            }
            return result;
        }


        /// <summary>
        /// 输出字符串
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="ii"></param>
        /// <param name="iii"></param>
        /// <returns></returns>
        public string OutputStr1(string ss, int ii, List<int> iii)
        {
            string result = ss + ii;
            if (iii != null && iii.Any())
            {
                result += string.Join("|", iii);
            }
            return result;
        }


    }

    /// <summary>
    /// 车辆行驶
    /// </summary>
    public class CarService {

        public event Notify notifier;//事件
        //public Notify notifier 相当于Class里面的Field，访问级别是public
        //public event Notify notifier 则相当于Property，访问级别是private

        public CarService(int petrol)
        {
            Petrol = petrol;
        }

        #region 被观察对象

        private int petrol = 0;
        public int Petrol
        {
            get { return petrol; }
            set
            {
                petrol = value;
                if (petrol < 10) //当petrol的值小于10时，出发警报
                {
                    if (notifier != null)
                    {
                        notifier.Invoke(Petrol);
                    }
                }
            }
        }

        #endregion

        public void Run(int speed)
        {
            int distance = 0;
            while (Petrol > 0)
            {
                Thread.Sleep(500);
                Petrol--;
                distance += speed;
                Console.WriteLine("Car is running... Distance is " + distance.ToString());
            }
        }
    }

    public class CarAlerterService
    {
        public CarAlerterService(CarService car)
        {
            car.notifier += new Notify(NotEnoughPetrol);
        }

        public void NotEnoughPetrol(int value)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("You only have " + value.ToString() + " gallon petrol left!");
            Console.ResetColor();
        }
    }
}
